<template>
  <div class="app-container">


<!--    <router-link :to="add_path">
      <el-button icon="el-icon-plus" circle type="success" size="mini"></el-button>
    </router-link>
    <hr> -->

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>

      <el-table-column align="center" label="排序[a-Z]" width="95" v-if="type != 'sign_in'">
        <template slot-scope="scope">
          <el-input type="number" min="0" v-model="scope.row.sort" @change="order(scope.$index)" />
        </template>
      </el-table-column>


      <el-table-column prop="title" label="标题" />

      <el-table-column prop="date" label="日期" v-if="type == 'sign_in'" />

      <el-table-column align="center" label="封面" width="300">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.thumb" fit="fill"></el-image>

        </template>
      </el-table-column>

      <el-table-column align="center" label="分类" width="300" v-if="type != 'sign_in' && type != 'guide' ">
        <template slot-scope="scope">
          <el-tag v-for="itm in scope.row.cate"> {{itm.title}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="标签" width="300" v-if="type != 'sign_in' && type != 'guide'">
        <template slot-scope="scope">
          <el-tag v-for="itm in scope.row.tag"> {{itm.title}}</el-tag>
        </template>
      </el-table-column>


      <el-table-column prop="copy_num" label="副本数" v-if="type != 'sign_in' && type != 'guide' " />
      <el-table-column prop="pv" label="点击量" />


      <el-table-column align="center" label="操作" width="300" fixed="right">
        <template slot-scope="scope">

          <el-button ref="btn" v-if="type == 'video' || type == 'sign_in'" icon="el-icon-video-play" circle type="primary" @click="playVideo(scope.row)">
          </el-button>


          <router-link :to="add_path+'?id=' + scope.row.id">
            <el-button type="" icon="el-icon-edit" circle></el-button>
          </router-link>

       <!--   <el-button type="danger" icon="el-icon-delete" circle @click="del(scope.$index, scope.row.id)"></el-button>
 -->
        </template>
      </el-table-column>

    </el-table>

    <el-pagination background layout="prev, pager, next" :page-size="per_page" :total="total" @current-change="go_page"></el-pagination>

    <el-dialog v-if="i > -1" title="确认操作框" :visible.sync="dialogVisible" width="30%">
      <span>确认删除[{{ list[i]['title'] }}]</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="od_del()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title :visible.sync="dialogPlay" width="60%" @close="closeDialog">
      <video :src="videoUrl" controls autoplay class="video" :ref="dialogVideo" width="100%"></video>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'article_index',
    props: {
      _type: {
        type: String,
        default: 'rich_text'
      },
      _add_path: {
        type: String,
        default: ''
      },

    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger'
        };
        return statusMap[status];
      }
    },
    data() {
      return {
        add_path: '',
        date: '',
        list: null,
        listLoading: true,
        i: -1,
        id: -1,
        page: 1,
        dialogVisible: false,
        total: 0,
        per_page: 0,
        type: 'rich_text',
        org_id: 0,
        videoUrl: '',
        dialogPlay: false,
        dialogVideo: false,
      };
    },
    created() {

      this.type = this._type
      this.add_path = this._add_path
      this.fetchData();
      this.$refs.btn.onclik(function() {
        if (this.$refs.dialogVideo.paused) {
          this.$refs.dialogVideo.play();
        } else {
          this.$refs.dialogVideo.pause();
        }
      });


    },

    closeDialog() {
      this.videoUrl = ""; //清空数据 关闭视频播放
    },
    methods: {
      playVideo(row) {
        this.dialogPlay = true;
        this.videoUrl = row.file_path;
      },

      order(i) {
        let data = {
          id: this.list[i]['id'],
          val: this.list[i]['sort'],
          field: 'sort'
        };
        this.request({
          url: '/admin/article/update',
          method: 'post',
          data: data
        }).then(e => {
          if (e.status) {
            this.$message({
              message: e.msg,
              type: 'success'
            });
          }
        });
      },
      del(i, id) {
        this.i = i;
        this.id = id;
        this.dialogVisible = true;
      },

      od_del() {
        this.request({
          url: '/admin/article/del',
          method: 'get',
          params: {
            id: this.id
          }
        }).then(e => {
          let ii = this.i
          this.i = -1
          this.list.splice(ii, 1)
          this.dialogVisible = false
        });
      },
      go_page(page) {
        this.page = page;
        this.fetchData();
      },
      reload() {
        this.page = 1
        this.fetchData()
      },
      fetchData() {
        const data = {
          page: this.page,
          type: this.type,
          org_id: this.org_id,
          date: this.date
        };
        this.listLoading = true;
        this.request({
          url: '/admin/article/index',
          method: 'POST',
          data
        }).then(response => {
          this.total = response.data.list.total;
          this.per_page = response.data.list.per_page;
          this.listLoading = false;
          this.list = response.data.list.data;
        });
      }
    }
  };
</script>

<style>
  .row_img {
    width: 50px;
    height: 50px;
  }

  .img_block {
    border-radius: 50%;
    width: 30px;
    height: 30px;
    vertical-align: middle;
  }

  .row_img {

    width: 80px;
    height: 80px;
    vertical-align: middle;
  }
</style>
